package com.hzjq.encrypt.encrypt;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 为计算CRC校验位的3DES算法
 */
public class TripleDES_CRC {

    public static String endocde(String str, String key) throws Exception {
        byte[] tmp = new byte[24];
        tmp = key.getBytes();
        SecretKey k = new SecretKeySpec(tmp, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, k);
        byte[] waitforencode_bytes = str.getBytes("UTF-8");
        byte[] encode_bytes = cipher.doFinal(waitforencode_bytes);
        return Base64.getInstance().encode(encode_bytes);
    }


    public static String decode(String str, String key) throws Exception {
        byte[] decode_byte = Base64.getInstance().decode(str);
        byte[] tmp = new byte[24];
        tmp = key.getBytes();
        SecretKey k = new SecretKeySpec(tmp, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.DECRYPT_MODE, k);
        byte[] decode_bytes = cipher.doFinal(decode_byte);
        return new String(decode_bytes, "UTF-8");
    }

}
